﻿using MediatR;
using ProduceBasicData.Api.Application.Command.ProductionManage;
using ProduceBasicData.Domain.ProductionManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.ProductionManage
{
    public class DeleteWorkCommandHandler : IRequestHandler<DeleteWorkCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<Work_order> workorder;

        public DeleteWorkCommandHandler(IBaseRespository<Work_order> workorder)
        {
            this.workorder = workorder;
        }

        public async Task<ApiResult<int>> Handle(DeleteWorkCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>()
            {
                Code = ApiEnum.Success,
                Msg = "工单删除成功"
            };
            var orderids=request.Ids.Split(',').Select(int.Parse).ToList();
            var order = workorder.GetAll().Where(o => orderids.Contains(o.Id)).ToList();

            foreach (var item in order)
            {
                item.Isdel = true;
            }
            result.Data = await workorder.UpdateRange(order);

            return await Task.FromResult(result);
        }
    }
}
